--
-- Author: yawei.zhu
-- Date: 2018-12-18 15:28:20
--
-- PvpChallengeBookView
--
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpChallengeBookScene
local arrKey = {"victoryReward", "goldCupReward", "silverCupReward", "copperCupReward", "failureReward"}

function ClassRef:init()
	self._ui["Panel/Panel_right/Button"]:addClickEventListener(function()
		executeFunc(self._onOperate, "showChallengeRankAward", self._id)
	end)

	self._ui.Button_close:addClickEventListener(function()
		self:closeView()
	end)

	self._iconRenders = {}
	self._iconTexts = {}
	local iconPanel = self._ui["Panel/Panel_right/Panel_item/Panel_icon"]
	for i = 1, 5 do
		local render = IconRender.create(iconPanel:getChildByName("icon"..i))
		render:setTipEnable(true)
		self._iconRenders[i] = render
		self._iconTexts[i] = iconPanel:getChildByName("Text_"..i)
	end
end

function ClassRef:updateView(playId)
	self._id = playId
	self._ui["Panel/Panel_right/tlt"]:setString(L("lua_code_text_278"))

	-- 优化
	display.setNodesMiddle(self._ui["Panel_right/Panel_text"], self._ui["Panel_right/Panel_text/Image"], self._ui["Panel_right/Panel_text/tlt"])

	-- 先查询是否属于pvp的 读配置 championship
	local extraReward = GD:queryChampionshipsConf(playId) 
	if extraReward then
		self._ui["Panel/Panel_left/Image"]:loadTexture(Res.teamtarget_pic .. extraReward.championBg .. ".png")
		self._ui["Panel/Panel_left/Text"]:setString(L(extraReward.championTips))
		self._ui["Panel/Panel_right/Panel_text/text"]:setString(L(extraReward.championDes))
	end
	-- 再查询是否属于活动的 读配置 event_mode
	if not extraReward then
		extraReward = GD:queryEventModeById(playId)
		if extraReward then
			self._ui["Panel/Panel_left/Image"]:loadTexture(Res.teamtarget_pic .. extraReward.eventBg .. ".png")
			self._ui["Panel/Panel_left/Text"]:setString(L(extraReward.eventTips))
			self._ui["Panel/Panel_right/Panel_text/text"]:setString(L(extraReward.eventDes))
		end
	end

	-- 挑战赛配置
	local challengeConf = GD:getChallengeInfo(playId)
	self._ui["Panel/Panel_left/tlt"]:setString(L(challengeConf.targetDesc)) 
	-- 替换\n
	local str = string.gsub(L(challengeConf.desc), "\\n", "\n")
	self._ui["Panel/Panel_right/Panel_item/Text"]:setString(str)

	-- 奖励
	local count = 0
	local arrIndex = {}
	for i = 1, 5 do
		local render = self._iconRenders[i]
		local text = self._iconTexts[i]
		local value = challengeConf[arrKey[i]]
		if value then
			count = count + 1
			arrIndex[count] = i
			render:setVisible(true)
			local reward = UD:parseRewardToBagItem(value)
            render:setData(reward)
			text:setVisible(true)
		else
			render:setVisible(false)
			text:setVisible(false)
		end
	end

	local midX = 175
	local intervalX = 88
	local midNum = (1 + count) / 2
	for i = 1, count do
		local index = arrIndex[i]
		local render = self._iconRenders[index]
		local text = self._iconTexts[index]
 		local centerX = midX - (midNum - i) * intervalX
 		text:setPositionX(centerX)
 		render:setPositionX(centerX - 39)
	end
end

return ClassRef
